package com.situ.student.view;

import java.util.List;
import java.util.Scanner;

import com.situ.student.controller.StudentController;
import com.situ.student.entity.Student;
import com.situ.student.exception.NameRepeatException;

public class StudentView {
	private Scanner scanner = new Scanner(System.in);
	private StudentController studentController = new StudentController();
	/*private StudentController studentController = null;
	
	public StudentView() {
		studentController = new StudentController();
	}*/
	
	public void showView() {
		while (true) {
			System.out.println("---学生信息管理学系统------");
			System.out.println("1、查询所有");
			System.out.println("2、添加");
			System.out.println("3、修改");
			System.out.println("4、删除");
			System.out.println("0、退出");
			System.out.println("请选择");
			int selected = scanner.nextInt();
			
			if (selected == 0) {
				System.out.println("退出程序");
				return;
			}
			
			
			switch (selected) {
			case 1://查询所有
				findAll();
				break;
			case 2://添加
				register();
				break;
			case 3://修改
				break;
			case 4://删除
				break;
			default:
				
				break;
			}
		}
	}

	private void register() {
		System.out.println("请输入用户名：");
		String name = scanner.next();
		System.out.println("请输入年龄：");
		int age = scanner.nextInt();
		System.out.println("请输入性别");
		String gender = scanner.next();
		boolean isSuccess = false ;
		try {
			isSuccess = studentController.register(name, age, gender);
		} catch (NameRepeatException e) {
			//e.printStackTrace();
			System.out.println(e.getMessage());
		}
		if (isSuccess) {
			System.out.println("注册成功");
		} else {
			System.out.println("注册失败");
		}
	}

	private void findAll() {
		List<Student> list = studentController.findAll();
		for (Student student : list) {
			System.out.println(student);
		}
	}
	
	
	
}
